package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.BrandType;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.PopMenuDate;

import java.util.List;

/**
 * Created by yang on 15/11/23.
 */
public class BrandListAdapter extends BaseAdapter {
    private Context mContext;
    private List<BrandType> num;
    private BrandType currentBrand;
    private PopMenuDate.IPopupWindowsConfirm mListener;

    public BrandListAdapter(Context context, List<BrandType> num,PopMenuDate.IPopupWindowsConfirm mListener){
        mContext = context;
        this.num = num;
        this.mListener = mListener;
    }
    @Override
    public int getCount() {
        return num.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_brand_item,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        holder.tv_date.setTag(position);
        holder.tv_id.setText((position + 1) + ".");
        final ViewHolder finalHolder = holder;
        holder.tv_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag = (int) v.getTag();
                currentBrand = num.get(tag);
                PopMenuDate popMenuDate2 = new PopMenuDate(true,mContext);
                popMenuDate2.setmListener(mListener);
                popMenuDate2.setTitle("选择日期");
                popMenuDate2.setMaxYear(MyTool.getCurrentYear());
                popMenuDate2.setMinYear(MyTool.getCurrentYear() - 4);
                // popMenuDate.setDate("2015-02-28");//默认为今天
                popMenuDate2.setTextViewOfShow(finalHolder.tv_date);
                popMenuDate2.setModel(currentBrand);
                popMenuDate2.show(finalHolder.tv_date, 0, 0);
            }
        });

        holder.et_type.setTag(position);
        final ViewHolder finalHolder1 = holder;
        holder.et_type.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                int tag = (int) v.getTag();
                currentBrand = num.get(tag);
                currentBrand.setType(finalHolder1.et_type.getText().toString().trim());
            }
        });

        BrandType brandType = num.get(position);

        String type = brandType.getType();
        if(type != null) {
            holder.et_type.setText(type);
        }

        String date = brandType.getDate();
        if(date != null){
            holder.tv_date.setText(date);
        }

        return convertView;
    }

    class ViewHolder {
        EditText et_type;
        TextView tv_date;
        TextView tv_id;

        public ViewHolder(View view){
            et_type = (EditText) view.findViewById(R.id.brand_type);
            tv_date = (TextView) view.findViewById(R.id.brand_date);
            tv_id = (TextView) view.findViewById(R.id.brand_id);
        }
    }
}
